<template>
  <n-modal v-model:show="isVisible" title="添加仓库" preset="dialog" @close="onClose" style="height: 400px;width: 500px">
    <n-form @submit.prevent="handleSubmit" label-width="120px" class="form-container">

      <div class="form-row" style="margin-top: 20px">
        <n-form-item label="仓库名称：" path="name">
          <n-input v-model:value="form.name" placeholder="请输入仓库名称" />
        </n-form-item>
        <n-form-item label="仓库地址：" path="address">
          <n-input v-model:value="form.address" placeholder="请输入仓库地址" />
        </n-form-item>
      </div>
      <div class="form-row">
        <n-form-item label="仓库类型：" path="type">
          <n-select v-model:value="form.type"
                    :options="typeOptions"
                    placeholder="选择仓库类型"
                    style="width: 180px"
                    filterable />
        </n-form-item>

        <n-form-item label="负责人：" path="storeManagerId">
          <n-select
            v-model:value="form.storeManagerId"
            :options="admins"
            style="width: 180px"></n-select>
        </n-form-item>
      </div>

      <!-- 提交按钮 -->
      <div class="submit-row">
        <n-form-item>
          <n-button type="primary" native-type="submit" @click="handleSubmit">提交</n-button>
        </n-form-item>
      </div>
    </n-form>
  </n-modal>
</template>

<script setup>
import {onMounted, ref, watch} from 'vue';
import {useRouter} from 'vue-router';
import {useUserStore} from "@/store/index.js";
import api from '@/api/admin'
import {List} from "echarts";
import {formatDate, formatDateTime} from "@/utils";
import {NCascader} from 'naive-ui'

const router = useRouter()
const loading = ref(true)
const userStore = useUserStore()
const id = userStore.getId();
const role=userStore.getRole();
const adminRole=role.toString();
const emit = defineEmits(['updatePage'])
const props = defineProps({
  show: {
    type: Boolean,
    required: true,
  },
  onClose: {
    type: Function,
    required: true,
  },
});

const isVisible = ref(props.show);
watch(() => props.show, (newVal) => {
  isVisible.value = newVal;
});
const admins=ref([])
const form = ref({
  name: '',
  address:'',
  type: '',
  storeManagerId:'',
});

const handleSubmit = async () => {
  try {
    const res = await api.addStore(
      {
        ...form.value,
        adminId:id,
        adminRole:adminRole,
      }
    )
    if(res.code === 200) {
      emit("updatePage")
    }
    else {
      $message.error(res.message)
    }
  } catch (err) {
    $message.error("添加失败！")
  }finally{
    clear()
    props.onClose();
  }
};
const queryAdminList = async () => {
  loading.value = true
  try {
    const res = await api.findAdminByRole({
      role: 'STOREADMIN'
    })
    if(res.code === 200) {
      res.data.map((item)=>{
        admins.value.push({
          label: item.name,
          value: item.id
        })
      })
    }
    else {
      $message.error(res.message)
    }
  } catch (err) {
    $message.error("获取管理员信息失败！")
  }finally{
    loading.value = false
  }
}

const typeOptions = [
  { label: '本地仓', value: '本地仓' },
	{ label: '海外仓', value: '海外仓' },
	{ label: '平台仓', value: '平台仓' },
];
const clear = () => {
  form.value.name='';
    form.value.type='';
    form.value.address='';
    form.value.storeManagerId='';
}
onMounted(() => {
  queryAdminList()
})
</script>

<style scoped>
.form-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 100%;
  position: relative;
}

.form-row {
  display: flex;
  gap: 20px;
  align-items: center;
  width: 100%;
}

.form-row > n-form-item {
  flex: 1;
}

.submit-row {
  justify-content: center;
  display: flex;
  width: 100%;
}
</style>
